package org.example.Controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RequestMapping("/chat")
@RestController
public class ChatClientController {
    @Autowired
    private ChatClient chatClient;
    @GetMapping("/call")
    public String generation(String userInput){
        return this.chatClient.prompt()
                //用户输入
                .user(userInput)
                //调用ai
                .call()
                //返回响应
                .content();
    }

    //格式化输出
    record Recipe(String dish, List<String> ingredients){}
    @GetMapping("/entity")
    public String entity(String userInput){
        Recipe recipe=chatClient.prompt()
                .user(String.format("请帮我生成%s的食谱",userInput))
                .call()
                .entity(Recipe.class);
        return recipe.toString();
    }
    @GetMapping(value="/stream",produces = "text/html;charset=utf-8")
    public Flux<String> stream(String userInput){
        return this.chatClient.prompt()
                //用户输入
                .user(userInput)
                //调用AI  流式输出
                .stream()
                //返回响应
                .content();
    }
}
